﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Common.Library;
using System.Collections.ObjectModel;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace OnlineVideos.Views
{
    public partial class OnlineImages : PhoneApplicationPage
    {
        public ObservableCollection<ImageSource> images = new ObservableCollection<ImageSource>();
        public int count = 0;
        public OnlineImages()
        {
            InitializeComponent();
            Loaded += OnlineImages_Loaded;
        }

        void OnlineImages_Loaded(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(Constants.WebSliceImagePath))
            {
                NavigationService.GoBack();
            }
            else
            {
                ImageList.ItemsSource = images;
                if (Constants.ImageUrls.Count > 0)
                {
                    performanceProgressBar.IsIndeterminate = true;
                    foreach (string url in Constants.ImageUrls)
                    {
                        string UrlText = string.Empty;
                        if (!url.ToLower().StartsWith("http://"))
                        {
                            if (url.ToLower().Contains("www."))
                                UrlText = "http://" + url;
                            else
                                UrlText = "http://www." + url;
                        }
                        else
                            UrlText = url;
                       
                        BitmapImage bi = new BitmapImage();
                        bi.UriSource = new Uri(UrlText, UriKind.RelativeOrAbsolute);
                        bi.CreateOptions = BitmapCreateOptions.None;
                        bi.ImageOpened += bi_ImageOpened;
                        bi.ImageFailed += bi_ImageFailed;
                    }
                }
            }
        }
        void bi_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {
            if (++count == Constants.ImageUrls.Count)
            {
                performanceProgressBar.IsIndeterminate = false;
                ImageList.SelectionChanged += ListBox_SelectionChanged_1;
            }
        }

        void bi_ImageOpened(object sender, RoutedEventArgs e)
        {
            images.Add((sender as BitmapImage) as ImageSource);
            if (++count == Constants.ImageUrls.Count)
            {
                performanceProgressBar.IsIndeterminate = false;
                ImageList.SelectionChanged += ListBox_SelectionChanged_1;
            }
        }
        private void ListBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/PhotoChooser.Xaml?ImageSource=" + ((BitmapImage)(sender as ListBox).SelectedItem).UriSource.ToString(), UriKind.Relative));
        }
    }
}